function [delta1,i] = deltaNL(thetaNL,Data)

% DeltaNL - Contraction Mapping for Random Coefficient Nested Logit
% Works like Logit apart for the weighting of the difference between
% calculated and observed shares
% Syntax:  f = deltaNL(thetaNL,Data)
%
% Inputs:
%    input1 - Non linear parameters
%    input2 - Data
%
% Outputs:
%    Mean Value Delta
%
% MAT-files required: mvaloldNL
% Subfunctions: NLShareCalculation
%

% Author: Laura Grigolon and Frank Verboven
% August 2012;

load mvalold
k   = 100;
km  = 1e-14;
i   = 0;
% if max(isnan(delta0))  == 1 || isreal(delta0) == 0
if max(isnan(delta0))  == 1
    deltastart         =  zeros(Data.nobs,1);
else
    deltastart         = delta0;
end

% Unpack
logobsshare              = Data.logobsshare;

while k > km
    % Market Share
    [~,~,sh,~,~,~,~,~,~] = NLShareCalculation(thetaNL,deltastart,Data);
    sh      = reshape((sum(sh,2)),[Data.nobs 1]);   % 2D market shares
    % Modified contraction mapping for the random coefficient nested logit
    % theta(1) is sigseg
    delta1  = deltastart+((1-max(thetaNL(2)))*(logobsshare-log(sh)));
    if max(isnan(delta1)) == 1
%         disp('No Convergence - delta calculation failed: OVERFLOW')
    break
    end
    
    if isreal(delta1) == 0
        break
    end
    
    i = i + 1;
    if i > 2500
%         disp('No Convergence - delta convergence failed')
        break
    else
    k           = max(abs(delta1-deltastart));
    deltastart  = delta1;
    end
end

%disp(['# of iterations for delta convergence:  ' num2str(i)])

if isreal(delta1)   == 0
    delta0          =  zeros(Data.nobs,1);
else
    delta0 = delta1;
end

save mvalold delta0